package constant.milk.cashbook.asset.real;

import java.util.ArrayList;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.util.MilkTextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class RealActivity extends SuperActivity
{
	private static final int REQUEST_REAL_INSERT = 0;
	private static final int REQUEST_REAL_UPDATE = 1;
	
	public static final String REAL_ID = "REAL_ID";
	
	private ListView listView;
	private Button addButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.realmoney_activity);
		
		setTitle("현금");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		addButton = (Button) findViewById(R.id.addButton);
	}

	@Override
	protected void setOnClick()
	{
		addButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, RealInsertActivity.class);
				startActivityForResult(intent, REQUEST_REAL_INSERT);
			}
		});
	}

	@Override
	protected void setData()
	{
		DBSelectQuery db = new DBSelectQuery(superContext, superDb);
		
		ArrayList<RealData> list = db.getRealList();
		
		listView.setOnItemClickListener(onItemClickListener);
		
		RealListAdapter adapter = new RealListAdapter(
				superContext, R.layout.normal_list_item, list);
		listView.setAdapter(adapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			RealData data = (RealData) arg0.getItemAtPosition(position);
			
			Intent intent = new Intent(superContext, RealUpdateActivity.class);
			intent.putExtra(REAL_ID, data.getId());
			startActivityForResult(intent, REQUEST_REAL_UPDATE);
		}
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_REAL_INSERT)
		{
			setData();
		}
		else if (requestCode == REQUEST_REAL_UPDATE)
		{
			setData();
		}
	}
	
	/**
	 * 현금 리스트 아답터
	 */
	private class RealListAdapter extends ArrayAdapter<RealData> 
	{
    	private ArrayList<RealData> list;
    	
		public RealListAdapter(Context context, int textViewResourceId, ArrayList<RealData> list) {
			super(context, textViewResourceId, list);
			this.list = list;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.normal_list_item, null);
			}
			
			RealData data = list.get(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.normalItemNameTextView);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(data.getName());
			
			return v;
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_ASSET_REALMONEY);
		startActivity(intent);
	}

}
